package com.solution.stack;

import java.util.Deque;
import java.util.LinkedList;

public class Solution {

    public int[] dailyTemperatures(int[] temperatures) {
        int len = temperatures.length;
        int[] res = new int[len];
        Deque<Integer> stack = new LinkedList<>();
        for ( int i = 0 ; i < len ; i++ ) {
            int temperature = temperatures[i];
            while ( !stack.isEmpty() && temperature > temperatures[stack.peek()] ) {
                int pop = stack.pop();
                res[pop] = i - pop;
            }
            stack.push(i);
        }
        return res;
    }
}
